package cn.new7as.cas5s.ui.login;


import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;

import androidx.annotation.NonNull;

import com.hjq.http.EasyHttp;
import com.hjq.http.listener.OnHttpListener;

import cn.new7as.cas5s.ui.slideshow.BaseActivity;
import cn.new7as.cas5s.R;
import cn.new7as.cas5s.tool.util.Utils;
import cn.new7as.cas5s.databinding.ActivityForgetpwdBinding;
import cn.new7as.cas5s.ui.easy.netapi.HttpData;
import cn.new7as.cas5s.ui.easy.netapi.FindPwdApi;
import cn.new7as.cas5s.ui.easy.netapi.FindSendApi;


public class ForgetPwdActivity extends BaseActivity {


    private ActivityForgetpwdBinding binding;


    @SuppressLint("MissingPermission")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityForgetpwdBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        setop("Forgot Password");

        binding.send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String ph = binding.phonenum.getText().toString();
                if (ph.length() == 10)
                    send(binding.phonenum.getText().toString());
                else
                    toa(getString(R.string.phone_wrong));
            }
        });

        binding.verifyme.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String ph = binding.phonenum.getText().toString();
                if (ph.length() == 10) {
                    String otp = binding.phoneotp.getText().toString();
                    if (otp.isEmpty()) {
                        toa(getString(R.string.otp_empty));
                    } else
                        find(ph, otp);
                } else
                    toa(getString(R.string.phone_wrong));
            }
        });
    }

    public static final int CHANGE_TIPS_TIMER_INTERVAL = 1000;
    private Handler mChangeTipsHandler = new Handler(Looper.getMainLooper());
    int i = 60;

    private void startMethod4() {
        binding.send.setEnabled(false);
        Runnable mChangeTipsRunnable = new Runnable() {
            @Override
            public void run() {
                binding.send.setText(i + "S");
                i--;
                if (i == 0) {
                    binding.send.setEnabled(true);
                    binding.send.setText("Resend");
                    binding.send.setTextColor(getResources().getColor(R.color.theme));
                } else
                    mChangeTipsHandler.postDelayed(this, CHANGE_TIPS_TIMER_INTERVAL);
            }
        };

        mChangeTipsHandler.post(mChangeTipsRunnable);
    }

    void send(String pho) {
        EasyHttp.post(this)
                .api(new FindSendApi(pho)).request(new OnHttpListener<Object>() {
                    @Override
                    public void onHttpSuccess(@NonNull Object result) {
                        binding.send.setTextColor(getResources().getColor(R.color.text));
                        i = 60;

                        startMethod4();
                    }

                    @Override
                    public void onHttpFail(@NonNull Throwable throwable) {

                    }
                });
    }

    void find(String pho, String ver) {
        EasyHttp.post(this)
                .api(new FindPwdApi(pho, ver)).request(new OnHttpListener<HttpData<String>>() {
                    @Override
                    public void onHttpSuccess(@NonNull HttpData<String> result) {

                        Utils.goextra(ForgetPwdActivity.this, SetNewPwdActivity.class,result.getResult());
                        finish();
                    }

                    @Override
                    public void onHttpFail(@NonNull Throwable throwable) {
                        deal(throwable);

                    }
                });
    }

}